﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 流程测试v1_1.组件
{
    public static class Log
    {
        public static void SaveLogToLogFile(string message, int ilogflag)
        {
            switch (ilogflag)
            {
                case 0: // 上传log
                    WriteLog(message, "软件log", "PLC交互记录");
                    break;
                case 1: // 设备状态上传记录log
                    WriteLog(message, "软件log", "软件状态记录");
                    break;

                case 11: // 图片上传记录
                    WriteLog(message, "软件log", "图片上传记录");
                    break;

                case 21: //产品流向记录
                    WriteLog(message, "软件log", "产品流向记录");
                    break;

                case 31: //PHM上传记录
                    WriteLog(message, "软件log", "设备运行状态记录");
                    break;
                case 32: //PHM上传记录
                    WriteLog(message, "软件log", "设备状态改变记录");
                    break;
                case 33: //PHM上传记录
                    WriteLog(message, "软件log", "PHM设备Log上传记录");
                    break;

                case 41:
                    WriteLog(message, "软件log", "历史报警记录");
                    break;
                case 42:
                    WriteLog(message, "软件log", "手动停机记录");
                    break;
                case 43:
                    WriteLog(message, "软件log", "产品抛料记录");
                    break;
                case 44:
                    WriteLog(message, "软件log", "权限操作记录");
                    break;

                case 51:
                    WriteLog(message, "软件log", "状态产能上传记录");
                    break;

                case 61:
                    WriteLog(message, "软件log", "Serin上传记录");
                    break;

                case 98: // 客户端交互记录Log
                    WriteLog(message, "软件log", "测试记录");
                    break;
                case 99: // 客户端交互记录Log
                    WriteLog(message, "软件log", "软件报错记录");
                    break;
                default:
                    WriteLog(message, "软件log", "其它记录");
                    break;
            }
        }
        public static void WriteLog(string message, string filepath, string filename)
        {
            try
            {
                string basePath = "D:" + "\\" + filepath + "\\";
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }
                basePath += DateTime.Today.ToString("yyyy-MM-dd") + "\\";
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }
                if (filename == "PLC交互记录" || filename == "图片上传记录")
                {
                    string currentTime = DateTime.Now.ToString("HHmmssfff");
                    int time = Convert.ToInt32(currentTime);
                    if (time > 000000000 && time <= 020000000)
                    {
                        filename += "_000000--020000";
                    }
                    else if (time > 020000000 && time <= 040000000)
                    {
                        filename += "_020000--040000";
                    }
                    else if (time > 040000000 && time <= 060000000)
                    {
                        filename += "_040000--060000";
                    }
                    else if (time > 060000000 && time <= 080000000)
                    {
                        filename += "_060000--080000";
                    }
                    else if (time > 080000000 && time <= 100000000)
                    {
                        filename += "_080000--100000";
                    }
                    else if (time > 100000000 && time <= 120000000)
                    {
                        filename += "_100000--120000";
                    }
                    else if (time > 120000000 && time <= 140000000)
                    {
                        filename += "_120000--140000";
                    }
                    else if (time > 140000000 && time <= 160000000)
                    {
                        filename += "_140000--160000";
                    }
                    else if (time > 160000000 && time <= 180000000)
                    {
                        filename += "_160000--180000";
                    }
                    else if (time > 180000000 && time <= 200000000)
                    {
                        filename += "_180000--200000";
                    }
                    else if (time > 200000000 && time <= 220000000)
                    {
                        filename += "_200000--220000";
                    }
                    else if (time > 220000000 && time <= 240000000)
                    {
                        filename += "_220000--000000";
                    }
                }
                string logFile = System.IO.Path.Combine(basePath, filename + ".log");
                bool writeBaseInfo = System.IO.File.Exists(logFile);

                if (!writeBaseInfo)
                {
                    using (FileStream fs = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                    {
                        //将字符串转成byte数组
                        byte[] byteFile = Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "   " + message + "\r\n");
                        fs.Write(byteFile, 0, byteFile.Length);
                        fs.Close();
                        fs.Dispose();
                    }
                }
                else
                {
                    using (FileStream fs = new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                    {
                        //将字符串转成byte数组
                        byte[] byteFile = Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "   " + message + "\r\n");
                        fs.Write(byteFile, 0, byteFile.Length);
                        fs.Close();
                        fs.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                string str = "ComUseFunction. WriteLog(string message, string filepath, string filename)出错：" + ex.ToString();
                Log.SaveLogToLogFile(str, 99);
            }
        }
    }
}
